<?php

namespace App\Http\Services;

use App\Models\School;

class SchoolService{

    const SCHOOL = 'school_';

    /**
     *获取school信息
     * @author lican<364548233@qq.com>
     */
    public static function getSchoolInfo($schoolCode){
        $key = self::SCHOOL . $schoolCode;
        $school = app('redis')->get($key);
        if(empty($school)){
            try{
                $school = School::where('code', $schoolCode)->first();
            }catch(\Exception $e){
                var_dump($e->getMessage());
            }
            if($school){
                $school = $school->toArray();
            }
            app('redis')->setex($key, 3600, json_encode($school));
        }else{
            $school = json_decode($school, true);
        }
        return $school;
    }

    /**
     *获取简短的主体列表
     * @author: lican<364548233@qq.com>
     */
    public static function getBrief(){
        $school = School::select('id', 'code', 'name')
            ->get()
            ->toArray();
        array_unshift($school, ['id' => 0, 'code' => '', 'name' => '全部']);
        return $school;
    }

}
